package com.mes.biz.service.impl;

import com.mes.biz.domain.BizPayable;
import com.mes.biz.domain.BizPurchase;
import com.mes.biz.domain.BizSupplier;
import com.mes.biz.domain.BizSupplierPayable;
import com.mes.biz.mapper.BizPayableMapper;
import com.mes.biz.mapper.BizPurchaseMapper;
import com.mes.biz.mapper.BizSupplierMapper;
import com.mes.biz.mapper.BizSupplierPayableMapper;
import com.mes.biz.service.BizPayableService;
import com.mes.biz.service.BizPurchaseService;
import com.mes.biz.service.BizSupplierPayableService;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

/**
 * 付账单Service业务层处理
 * @author hwg
 * @email hwg
 * @date 2023-04-30
 */
@Service
@Transactional(readOnly = true)
public class BizPayableServiceImpl extends BaseServiceImpl<BizPayableMapper, BizPayable> implements BizPayableService {

    @Resource
    private BizSupplierPayableMapper bizSupplierPayableMapper;
    @Resource
    private BizPayableMapper bizPayableMapper;
    @Resource
    private BizSupplierMapper bizSupplierMapper;
    @Resource
    private BizPurchaseMapper bizPurchaseMapper;
    @Resource
    private BizSupplierPayableService bizSupplierPayableService;

    @Override
    public List<BizPayable> getBizPayableByPurchaseCode(String purchaseCode) {
        return bizPayableMapper.getBizPayableByPurchaseCode(purchaseCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createPayable(BizPayable bizPayable) {
        Random random = new Random();
        //把随机生成的数字转成字符串
        String str = String.valueOf(random.nextInt(9));
        for (int i = 0; i < 5; i++) {
            str += random.nextInt(9);
        }
        bizPayable.setPayCode("PAY-" + str);
        String tenantId = SecurityUtils.getTenantId();
        bizPayable.setTenantId(tenantId);
        BizSupplier bizSupplier = bizSupplierMapper.getBizSupplierBySupplierCode(bizPayable.getSupplierCode());
        bizPayable.setSupplierName(bizSupplier.getSupplierName());
        BizSupplierPayable bizSupplierPayable = bizSupplierPayableMapper.getBizSupplierPayableBySupplierCode(bizPayable.getSupplierCode());
        if(bizPayable.getPayType().equals(BaseEntity.PARCHASE)){
            bizPayable.setHasAmount(bizSupplierPayable.getHasAmount());
        }
        boolean res = save(bizPayable);
        if(res) {
            if(bizPayable.getPayType().equals(BaseEntity.PARCHASE)){
                BizPurchase bizPurchase = bizPurchaseMapper.getBizPurchaseByPurchaseCode(bizPayable.getPurchaseCode());
                bizPurchase.setPayAmount(bizPurchase.getPayAmount().add(bizPayable.getPayAmount()));
                bizPurchaseMapper.update(bizPurchase);
//                bizSupplierPayable.setHasAmount(bizSupplierPayable.getHasAmount().subtract(bizPayable.getPayAmount()));
//                bizSupplierPayable.setPayAmount(bizSupplierPayable.getPayAmount().add(bizPayable.getPayAmount()));
//                bizSupplierPayableMapper.update(bizSupplierPayable);
            }
            if (StringUtils.isNull(bizSupplierPayable)){
                bizSupplierPayable =new BizSupplierPayable();
                bizSupplierPayable.setSupplierCode(bizPayable.getSupplierCode());
                bizSupplierPayable.setSupplierName(bizSupplier.getSupplierName());
                bizSupplierPayable.setPayAmount(bizPayable.getPayAmount());
                bizSupplierPayable.setHasAmount(BigDecimal.ZERO.subtract(bizPayable.getPayAmount()));
                bizSupplierPayable.setTenantId(tenantId);
                bizSupplierPayableService.save(bizSupplierPayable);
            }else {
                if (bizSupplierPayable.getPayAmount() != null){
                    bizSupplierPayable.setPayAmount(bizSupplierPayable.getPayAmount().add(bizPayable.getPayAmount()));
                }else {
                    bizSupplierPayable.setPayAmount(bizPayable.getPayAmount());
                }
                if (bizSupplierPayable.getHasAmount()!=null){
                    bizSupplierPayable.setHasAmount(bizSupplierPayable.getHasAmount().subtract(bizPayable.getPayAmount()));
                }else {
                    bizSupplierPayable.setHasAmount(BigDecimal.ZERO.subtract(bizPayable.getPayAmount()));
                }
                bizSupplierPayableMapper.update(bizSupplierPayable);
            }
        }
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePayable(BizPayable bizPayable) {
        int update = bizPayableMapper.update(bizPayable);
        if(update > 0) {
            BizSupplierPayable bizSupplierPayable = bizSupplierPayableMapper.getBizSupplierPayableBySupplierCode(bizPayable.getSupplierCode());
            bizSupplierPayable.setAdvanceAmount(bizPayable.getPayAmount());
            bizSupplierPayableMapper.update(bizSupplierPayable);
            return true;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletePayable(String id) {
        BizPayable bizPayable = bizPayableMapper.get(id);
        String purchaseCode = bizPayable.getPurchaseCode();
        String payType = bizPayable.getPayType();
        BigDecimal payAmount = bizPayable.getPayAmount();
        String supplierCode = bizPayable.getSupplierCode();
        int delete = bizPayableMapper.delete(bizPayable);
        if(delete > 0) {
            BizSupplierPayable bizSupplierPayable = bizSupplierPayableMapper.getBizSupplierPayableBySupplierCode(supplierCode);
            if(payType.equals(BaseEntity.PARCHASE)){
                BizPurchase bizPurchase = bizPurchaseMapper.getBizPurchaseByPurchaseCode(purchaseCode);
                bizPurchase.setPayAmount(bizPurchase.getPayAmount().subtract(payAmount));
                bizPurchaseMapper.update(bizPurchase);
            }
            bizSupplierPayable.setPayAmount(bizSupplierPayable.getPayAmount().subtract(payAmount));
            bizSupplierPayable.setHasAmount(bizSupplierPayable.getHasAmount().add(payAmount));
            bizSupplierPayableMapper.update(bizSupplierPayable);
            return true;
        }
        return false;
    }
}
